﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace LMSemir.Common
{
    /// <summary>
    /// 通用处理工具
    /// </summary>
    public class WebUtils
    {
        /// <summary>
        ///  获取用户Ip地址(包括获取Wap网关IP地址)
        /// </summary>
        public static string UserClientIP
        {
            get
            {
                try
                {
                    if (HttpContext.Current == null
                        || HttpContext.Current.Request == null
                         || HttpContext.Current.Request.ServerVariables == null)
                        return "";
                    string customerIP = "";
                    customerIP = HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                    if (!string.IsNullOrEmpty(customerIP))
                    {
                        return customerIP;
                    }
                    if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                    {
                        customerIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                        if (customerIP == null)
                            customerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }
                    else
                    {
                        customerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }
                    if (string.Compare(customerIP, "unknown", true) == 0)
                        return HttpContext.Current.Request.UserHostAddress;
                    return customerIP;

                }
                catch
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 代理服务信息
        /// </summary>
        public static string SquidForwardIp
        {
            get
            {
                try
                {
                    string forwordList = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


                    if (string.IsNullOrEmpty(forwordList))
                        return string.Empty;


                    string[] ipList = forwordList.Split(new[] { ',' });
                    if (ipList.Length >= 2)
                    {
                        return ipList[0];
                    }


                    if (ipList.Length == 1)
                    {
                        return ipList[0];
                    }

                    if (!forwordList.StartsWith("10."))
                    {
                        return forwordList;
                    }
                }
                catch (Exception)
                {
                    return string.Empty;
                }


                return string.Empty;
            }
        }

        #region 获取指定Form值
        /// <summary>
        /// 获得指定参数值
        /// </summary>
        /// <param name="pramaName">参数名</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static int GetFormValue(string pramaName, int defaultValue)
        {
            int ret = defaultValue;

            try
            {
                string pramaValue = HttpContext.Current.Request.Form[pramaName];

                if (!string.IsNullOrEmpty(pramaValue))
                {
                    ret = int.Parse(pramaValue);
                }
            }
            catch
            {
            }

            return ret;
        }


        /// <summary>
        /// 获得指定参数值
        /// </summary>
        /// <param name="pramaName">参数名</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static string GetFormValue(string pramaName, string defaultValue)
        {
            string ret = defaultValue;

            try
            {
                string pramaValue = HttpContext.Current.Request.Form[pramaName];

                if (!string.IsNullOrEmpty(pramaValue))
                {
                    ret = pramaValue;
                }
            }
            catch
            {
            }

            return ret;
        }

        /// <summary>
        /// 获得指定参数值
        /// </summary>
        /// <param name="pramaName">参数名</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static decimal GetFormValue(string pramaName, decimal defaultValue)
        {
            decimal ret = defaultValue;

            try
            {
                string pramaValue = HttpContext.Current.Request.Form[pramaName];

                if (!string.IsNullOrEmpty(pramaValue))
                {
                    ret = Decimal.Parse(pramaValue);
                }
            }
            catch
            {
            }

            return ret;
        }

        /// <summary>
        /// 获得指定参数值
        /// </summary>
        /// <param name="pramaName">参数名</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static DateTime GetFormValue(string pramaName, DateTime defaultValue)
        {
            DateTime ret = defaultValue;

            try
            {
                string pramaValue = HttpContext.Current.Request.Form[pramaName];

                ret = DateTime.Parse(pramaValue);
            }
            catch
            {
            }

            return ret;
        }
        #endregion

        #region 获得指定参数值

        /// <summary>
        /// 获得指定参数值
        /// </summary>
        /// <param name="pramaName">参数名</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static int GetPramaValue(HttpContext context, string pramaName, int defaultValue)
        {
            int ret = defaultValue;

            try
            {
                string pramaValue = context.Request[pramaName];

                if (!string.IsNullOrEmpty(pramaValue))
                {
                    ret = int.Parse(pramaValue);
                }
            }
            catch
            {
            }

            return ret;
        }

        public static int GetPramaValue(string pramaName, int defaultValue)
        {
            int ret = defaultValue;

            try
            {
                string pramaValue = HttpContext.Current.Request.QueryString[pramaName];

                if (!string.IsNullOrEmpty(pramaValue))
                {
                    ret = int.Parse(pramaValue);
                }
            }
            catch
            {
            }

            return ret;
        }

        /// <summary>
        /// 获得指定参数值
        /// </summary>
        /// <param name="pramaName">参数名</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static string GetPramaValue(string pramaName, string defaultValue)
        {
            string ret = defaultValue;

            try
            {
                string pramaValue = HttpContext.Current.Request.QueryString[pramaName];

                if (!string.IsNullOrEmpty(pramaValue))
                {
                    ret = HttpContext.Current.Server.UrlDecode(pramaValue);
                }
            }
            catch
            {
            }

            return ret;
        }

        /// <summary>
        /// 获得指定参数值
        /// </summary>
        /// <param name="pramaName">参数名</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static DateTime GetPramaValue(string pramaName, DateTime defaultValue)
        {
            DateTime ret = defaultValue;

            try
            {
                string pramaValue = HttpContext.Current.Request.QueryString[pramaName];

                ret = DateTime.Parse(pramaValue);
            }
            catch
            {
            }

            return ret;
        }
        #endregion

        #region 字符串截取
        public static string GetSubString(string sourceString, int length)
        {
            if (string.IsNullOrEmpty(sourceString))
            {
                return sourceString = "暂无内容";
            }
            if (sourceString.Length > length)
            {
                return sourceString.Substring(0, length) + "...";
            }
            return sourceString;
        }
        #endregion

        #region 过滤非法字符串
        public static string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
              RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&("")", "",
              RegexOptions.IgnoreCase);

            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

            return Htmlstring;
        }
        #endregion
    }
}
